package Atop100;

public class FindZhongWei {

    public  int find(int[] nums1,int[] nums2){
        int ans=0;
        boolean  flag=false;
        int len1 = nums1.length;
        int len2 = nums2.length;
        int len=len1+len2;
        if (len%2==0){
            flag=true;
        }
        int halfLen=len/2;
        int i=0;
        int j=0;
        int cur=0;
        int[] ints = new int[len];
        while (i<len1&&j<len2){
            if (nums1[i]<=nums1[j]){
                ints[cur++]=nums1[i++];
            }else {
                ints[cur++]=nums2[j++];
            }
            if (cur>halfLen){
                if (flag){
                    ans=(ints[cur-1]+ints[cur-2])/2;
                    return  ans;
                }else {
                    ans=ints[cur-1];
                    return  ans;
                }
            }

        }
        while (i<len1){
            ints[cur++]=nums1[i++];
            if (cur>halfLen){
                if (flag){
                    ans=(ints[cur-1]+ints[cur-2])/2;
                    return  ans;
                }else {
                    ans=ints[cur-1];
                    return  ans;
                }
            }

        }
        while (j<len2){
            ints[cur++]=nums1[j++];
            if (cur>halfLen){
                if (flag){
                    ans=(ints[cur-1]+ints[cur-2])/2;
                    return  ans;
                }else {
                    ans=ints[cur-1];
                    return  ans;
                }
            }
        }
        return  ans;


    }
}
